package gcj2008.qual;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.korifey.ffalg.Pair;
import org.korifey.ffalg.Sorts;
import org.korifey.ffalg.Pair.PairComparator;

import common.AbstractRunner;

public class B extends AbstractRunner {
	
	int parseTime(String time) {
		String[] tmp = time.split(":");
		return Integer.parseInt(tmp[0]) * 60 + Integer.parseInt(tmp[1]);
	}	
	
	public void handleCase(int caseNumber) throws Exception {
		int turnaround = scanner.nextInt();
		int na = scanner.nextInt();
		int nb = scanner.nextInt();
		
		scanner.nextLine();
		
		Pair<Boolean, Pair<Integer,Integer>>[] timetable = new Pair[na+nb];		
		for (int i=0; i<na; i++) {
			String[] unparsedTime = scanner.nextLine().split(" ");
			timetable[i] = Pair.of(false, Pair.of(parseTime(unparsedTime[0]), parseTime(unparsedTime[1])));			
		}		
		for (int i=0; i<nb; i++) {
			String[] unparsedTime = scanner.nextLine().split(" ");
			timetable[i+na] = Pair.of(true, Pair.of(parseTime(unparsedTime[0]), parseTime(unparsedTime[1])));			
		}
		Arrays.sort(timetable, new PairComparator(new Sorts.ZeroComparator(), Pair.DEFAULT_COMPARATOR, true));
		
		boolean[] used = new boolean[na+nb];
		int nused = 0;
		int resA = 0;
		int resB = 0;
		while (nused < na + nb) {
			int time = 0;
			boolean prev = false;
			int idx = 0;
			for (; idx<na+nb; idx++) {
				if (!used[idx]) {					
					prev = !timetable[idx].A();
					if (prev) resA++; else resB++;
					
					break;
				}
			}
			
			for (; idx < na+nb; idx++) {
				if (!used[idx] && prev != timetable[idx].A() && time <= timetable[idx].B().A()) {
					used[idx] = true;
					nused++;
					prev = !prev;
					time = timetable[idx].B().B() + turnaround;
				}
			}				
		}
		
		out.write(resA+" "+resB+"\n");
	}

	public static void main(String[] args) throws Exception {
		new B().init(SIZE.large).execute();
	}
		
}
